import 'dart:io';

import 'package:flutter/material.dart';
import 'package:image_cropper/image_cropper.dart';
import 'package:image_picker/image_picker.dart';
import 'package:trollee/app/core/core.dart';

class ImageClipPage extends StatefulWidget {
  const ImageClipPage({Key? key}) : super(key: key);

  @override
  State<ImageClipPage> createState() => _ImageClipPageState();
}

class _ImageClipPageState extends State<ImageClipPage> {

  String path = '';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('图像裁剪'),
      ),
      body: Container(child: Image.file(File(path))),
      floatingActionButton: FloatingActionButton(
        onPressed: _select,
        child: const Icon(Icons.add),
      ),
    );
  }

  _select() async {
    final ImagePicker _picker = ImagePicker();
    // Pick an image
    final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
    if (image != null) {
      CroppedFile? croppedFile = await ImageCropper().cropImage(
        sourcePath: image.path,
        aspectRatioPresets: [
          CropAspectRatioPreset.square,
          CropAspectRatioPreset.ratio3x2,
          CropAspectRatioPreset.original,
          CropAspectRatioPreset.ratio4x3,
          CropAspectRatioPreset.ratio16x9
        ],
        uiSettings: [
          AndroidUiSettings(
              toolbarTitle: '裁剪',
              toolbarColor: AppColors.primary_color,
              toolbarWidgetColor: Colors.white,
              initAspectRatio: CropAspectRatioPreset.original,

              lockAspectRatio: false),
          IOSUiSettings(
            title: '裁剪',
          ),
          WebUiSettings(
            context: context,
          ),
        ],
      );
      if(croppedFile != null) {
        path = croppedFile.path;
      }
      setState(() {

      });
    } else {}
  }
}
